package com.example.controller;

import com.example.entity.EasybuyUser;
import com.example.service.EasybuyUserService;
import com.example.util.RespBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@CrossOrigin
@RestController
@RequestMapping(value = "/new")
public class EasybuyUserController {

    @Autowired
    public EasybuyUserService easybuyUserService;

    @PostMapping(value = "login")
    public RespBean login(@RequestParam(name = "name")String name,
                          @RequestParam(name = "pwd")String pwd){

        EasybuyUser login = easybuyUserService.login(name,pwd);
        if (login != null){
            return RespBean.ok("登陆成功",login);
        }else {
            return RespBean.error("登陆失败");
        }
    }

    @ResponseBody
    @PostMapping(value = "/register")
    public RespBean adduser(@RequestParam("loginName") String loginName,
                            @RequestParam("userName") String userName,
                            @RequestParam("password") String password,
                            @RequestParam("sex") Integer sex,
                            @RequestParam("identityCode") String identityCode,
                            @RequestParam("email") String email,
                            @RequestParam("mobile") String mobile){
        EasybuyUser easybuyUser=new EasybuyUser();
        easybuyUser.setLoginname(loginName);
        easybuyUser.setUsername(userName);
        easybuyUser.setPassword(password);
        easybuyUser.setSex(sex);
        easybuyUser.setIdentitycode(identityCode);
        easybuyUser.setEmail(email);
        easybuyUser.setMobile(mobile);
        int register = easybuyUserService.register(easybuyUser);
        if (register > 1){
            return RespBean.ok("登陆成功",register);
        }else {
            return RespBean.error("登陆失败");
        }

    }
}